package com.lagou;

import com.lagou.bean.ComsumerComponet;
import com.lagou.bean.MonitorExecutor;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;



public class AnnotationConsumerMain {

    public static void main(String[] args) throws  Exception {
        System.out.println("------consumer开始启动-------");
        AnnotationConfigApplicationContext  context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();

        /**
         * 设置线程池
         *  延迟时间 上一次运行结束以后间隔时间 秒
         */
        Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(new MonitorExecutor(),5,5, TimeUnit.SECONDS);

        // 获取消费者组件
        ComsumerComponet  service = context.getBean(ComsumerComponet.class);

        //创建线程池 线程池大小2
       Executor executorService1 = Executors.newFixedThreadPool(10);
        //执行method A
       executorService1.execute(new Runnable() {
            @Override
            public void run() {
                //设定一分钟执行2000次
                while (true){
                    try {
                        System.out.println("开始执行方法 method A。。。。。"+service.method("A"));
                        Thread.sleep(30);//一分钟==60000毫秒 sleep 30毫秒 刚刚好2000
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        //执行method B
        Executor executorService2 = Executors.newFixedThreadPool(10);
        executorService2.execute(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        System.out.println("开始执行方法 method B。。。。。"+service.method("B"));
                        Thread.sleep(30);//一分钟==60000毫秒 sleep 30毫秒 刚刚好2000
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        });

        //执行method C
        Executor executorService3 = Executors.newFixedThreadPool(10);
        executorService3.execute(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        System.out.println("开始执行方法 method C。。。。。"+service.method("C"));
                        Thread.sleep(30);//一分钟==60000毫秒 sleep 30毫秒 刚刚好2000
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        });

    }
    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.lagou.bean")
    @EnableDubbo
    static  class  ConsumerConfiguration{

    }
}
